package com.ncdnapp.core.filter;

import com.ncdnapp.core.entity.Response;
import com.ncdnapp.core.entity.dto.PageResultDTO;
import org.noear.solon.annotation.Component;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Handler;
import org.noear.solon.core.route.RouterInterceptor;
import org.noear.solon.core.route.RouterInterceptorChain;

@Component(index = 0)
public class GlobalResultInterceptor implements RouterInterceptor {
    @Override
    public void doIntercept(Context ctx, Handler mainHandler, RouterInterceptorChain chain) throws Throwable {
        chain.doIntercept(ctx, mainHandler);
    }

    @Override
    public Object postResult(Context ctx, Object result){
        if (result instanceof Throwable) {
            return result;
        }
        boolean flag = false;
        if (result instanceof Response) {
            flag = true;
        } else if (result instanceof PageResultDTO) {
            flag = true;
        }
        if (!flag) {
            result = Response.success(result);
        }
        return result;
    }
}
